<?php

namespace app\Admin\controller;

use app\Admin\model\System;
use app\Admin\model\User;
use think\Controller;
use think\Db;
use think\Request;
use think\Validate;
use think\View;

class Systems extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $sys = new System();
        $data = $sys->getAll();
        return view('show', ['data' => $data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $sys =new System();
        $data = $sys->getOne($id);
        return \view('detail',['data'=>$data]);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $data = $request->except('/admin/systems/update_html');
        $sys =new System();
        $sys->active($data);
        return redirect('Systems/index');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $sys = new System();
        $res = $sys->delSys($id);
        if (!$res) {
            echo "<script>alert('删除失败')</script>";
        }
        return redirect('Systems/index');
    }

    public function tologin()
    {
        return view('login');
    }

    public function login()
    {
        $data = \request()->param();
        $arr = [];
        $arr['password'] = $data['password'];
        $datas = [
            'username' => $data['inputname'],
            'password' => $data['password'],
            '__token__' => $data['__token__']
        ];
        $rules = [
            'username' => 'require',
            'password' => 'require',
            '__token__' => 'token'
        ];
        $vaildate = new Validate($rules);
        $res = $vaildate->check($datas);
        if ($res != true) {
            dump($res);
            die();
        }
        $phone = '/^1[3,5,8]\d{9}$/';
        if (preg_match($phone,$data['inputname'])) {
            $arr['phone'] = $data['inputname'];
        } else {
            $arr['username'] = $data['inputname'];
        }
        $user = new User();
        $data = $user->getUser($arr);
        if ($data) {
            return redirect('Systems/index');
        } else {
            return \view('login');
        }
    }

    public function execl()
    {
        header('Content-Type:application/vnd.ms-excel');
        header("Content-Disposition:attachment;filename=info.xls");
        $data = Db::table('system')->select();
        $head = [];
        foreach ($data as $value) {
            foreach ($value as $key => $v) {
                $head [] = $key;
            }
            break;
        }
        echo "<table border='1'>";
        echo "<tr>";
        foreach ($head as $v) {
            echo "<th>" . $v . "</th>";
        }
        echo "</tr>";
        foreach ($data as $v) {
            echo "<tr>";
            foreach ($v as $value) {
                echo "<td>" . $value . "</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
}
